package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author 宋
 * @since 2022-06-06
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取登录验证码
     * Request URL: http://127.0.0.1:8080/user/sendMsg
     * Request Method: POST
     */
    @PostMapping("/sendMsg")
    public void code(@RequestBody User user, HttpSession session) {
        //用工具类生成验证码 -->> 手机号+验证码 存入redis数据库
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        //存入redis数据库
        redisTemplate.opsForValue().set(user.getPhone(), code.toString(), 5, TimeUnit.MINUTES);


        //session.setAttribute(user.getPhone(), code);
        log.info("code = {}", code);
    }

    /**
     * 用户登录验证
     * Request URL: http://127.0.0.1:8080/user/login
     * Request Method: POST
     */
    @PostMapping("/login")
    public R<String> login(@RequestBody User user, HttpSession session) {
        // 1 .用户输入的code 和 redis数据库中存的进行验证码校验
        String code = user.getCode();

        String code2 = redisTemplate.opsForValue().get(user.getPhone()).toString();
        //String code2 = session.getAttribute(user.getPhone()).toString();
        // 2 . 登录成功后存起来 防止被过滤器拦截  -->> 再判断是否是新用户  -->> 1新用户注册
        if (code.equals(code2)) {
            log.info("登录成功");
            // 2 . 1 新用户 ?
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone, user.getPhone());
            User user1 = userService.getOne(queryWrapper);

            if (user1 == null) {
                //不是新用户 -->> 注册
                user.setStatus(1);  //设置用户状态
                userService.save(user);
                log.info("新用户注册成功 -->> {}", user);
                //把用户id存入 session域中  //TODO 新用户
                session.setAttribute("user", user.getId());
                redisTemplate.delete(user.getPhone());
                return R.success("登录成功");
            } else {
                //存 老用户id到 session域中

                session.setAttribute("user", user1.getId());
                redisTemplate.delete(user.getPhone());
                return R.success("登录成功");
            }
        }
        return R.error("登录失败");


    }

}

